package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DeleteUser
 */
@WebServlet("/DeleteUser")
public class DeleteUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String user= request.getParameter("username");
		
		if (user != "") {
			 
				try {
				Class.forName("org.sqlite.JDBC");
				String url = "jdbc:sqlite:c:/Users/JayeM/workspace/MyWebProject/MyWebProject/WebContent/database/database.db";
				 		
				Connection con =  DriverManager.getConnection(url);
				String sql = "select username from tbluserinfo where username="+"'"+user+"'";
				String sql1 = "delete from tbluserinfo where username="+"'"+user+"'";
				Statement pstmt = con.createStatement();
				ResultSet rlt= pstmt.executeQuery(sql);
				 		
				while (rlt.next()) {
				String name = rlt.getString("username");
				 	        
				if (user.equals(name)) {
				 Statement pstmt1 = con.createStatement();
				 pstmt1.executeUpdate(sql1);
				 pstmt1.close();
				}
				 			
			}
				 pstmt.close();
				
				 	
			} catch (SQLException e) {
				e.printStackTrace();
				 		
				 		
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
	}
		
		request.getRequestDispatcher("listadmin.jsp").forward(request,response);
        return;
		}		
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
